package com.cmy.framework.room.test

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.cmy.framework.room.BaseRoomDatabase
import com.cmy.framework.room.test.dao.TestDao
import com.cmy.framework.room.test.entity.TestEntity

/**
 * @name:
 *  DBFactry
 * @package:
 *  com.cmy.test.mvp.dao
 * @date:
 *  2023/4/2611:12
 * @author
 *    cmy
 * @description
 *
 */
@Database(entities = [TestEntity::class], version = 1, exportSchema = false)
abstract class DBFactory : BaseRoomDatabase() {

    abstract fun getTestDao(): TestDao
    companion object{
        private const val DB_NAME = "test.db"

        @Volatile
        private var dbFactory: DBFactory? = null
        @Synchronized
        fun getInstance(context: Context):DBFactory{
            if (dbFactory == null){
                dbFactory = create(context)
            }
            return dbFactory!!
        }

        private fun create(context: Context):DBFactory{
            return Room.databaseBuilder(context,DBFactory::class.java, DB_NAME).build()
        }

    }
}